<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url', 'form'));
		$this->load->library('session');
		$this->load->database();
	}
	
	public function index()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('content', 'Content', 'required');
		$this->form_validation->set_rules('author', 'Author', 'required');
		
		if($this->form_validation->run() == false)
		{
			$post_data = $this->db->get('post')->result();
			$config['post_data'] = $post_data;
			$this->load->view('post_view', $config);
		}
		else
		{
			$this->add_action();
		}	
					
	}
	
	public function add_action()
	{
		$insert_data = array(
			'title' => $this->input->post('title'),
			'content' => $this->input->post('content'),
			'author' => $this->input->post('author'),
			'created' => date('Y-m-d H:i:s'),
		);
		$this->db->insert('post', $insert_data);
		// echo anchor('form', 'back to form');
		$this->session->set_flashdata('flash_message', 'insert sucess');
		redirect('post', 'refresh');
	}
	
	public function post_list($id = NULL)
	{
		if ($id !== NULL)
		{
			$post = $this->db->get_where('post', array('id'=>$id))->result();
			$config['show_comments'] = 1;			
			$post_comments = $this->db->get_where('comments', array('post_id'=>$id, 'parent'=>0))->result();
			
			// get reply comments(parent != 0)
			$comment_reply_record = $this->db->get_where('comments', array('parent != '=>0))->result();
			$comment_reply = array();
			foreach ($comment_reply_record as $comment)
			{
				$comment_reply[$comment->parent][] = array('author'=>$comment->author, 'site'=>$comment->site, 'content'=>$comment->content);
			}
		}
		else
		{
			$post = $this->db->get('post')->result();
			$config['show_comments'] = 0;
			$post_comments = '';
			$comment_reply = '';
		}
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('author', 'Your name', 'required');
		$this->form_validation->set_rules('content', 'Your Comments', 'required');
		$this->form_validation->set_rules('site', 'Your Site', 'prep_url');
		$this->form_validation->set_rules('parent', 'System error, please try again', 'required');
		if($this->form_validation->run() == False)
		{
			$config['post'] = $post;
			$config['post_comments'] = $post_comments;
			$config['comment_reply'] = $comment_reply;
			$this->load->view('post_list', $config);
		}
		else
		{
			$this->post_comments();
		}	
	}
	
	public function post_delete($id = NULL)
	{
		if ($id == NULL)
			return;
		$this->db->delete('post', array('id' => $id));	
		$this->session->set_flashdata('flash_message', 'delete sucess');
		redirect('post', 'refresh');	
	}

	public function post_comments()
	{
			$insert_data = array(
				'author' => $this->input->post('author'),
				'site' => $this->input->post('site'),
				'content' => $this->input->post('content'),
				'parent' => $this->input->post('parent'),
				'post_id' => $this->input->post('post_id'),
				'created' => date('Y-m-d H:i:s'),
			);
			// print_r($insert_data);
			$this->db->insert('comments', $insert_data);
			$this->session->set_flashdata('flash_message', 'thanks for your comments');
			redirect('post/post_list/' . $this->input->post('post_id'), 'refresh');	
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */